var serveUrl = require('../../../utils/util.js').serveUrl
var app = getApp()

// pages/home/myInfos/myInfos.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    yearNameArr:["0-1年","1-3年","3-5年","5-7年","7-10年","10年以上"],
    yearIndex:"",
    selectAllStatus:false,
    recruitId:"",
    showModalStatus: false,
    userInfos:[]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
  console.log(options);
  // 判断是保存信息还是投简历
  var canshuData = JSON.parse(options.canshu)
  console.log(canshuData)
  if (canshuData[0].recruitId!=""){
  wx.setNavigationBarTitle({
    title: canshuData[0].title//页面标题为路由参数
  });
  that.setData({
    recruitId: canshuData[0].recruitId,
    selectAllStatus:true
  })
  }else{
    wx.setNavigationBarTitle({
      title: canshuData[0].title//页面标题为路由参数
    });
    that.setData({
      selectAllStatus: false
    })
  }
  that.getJianLiInfo()
  },
  // 选择工作年限
  bindYearChange(e){
    this.setData({
      yearIndex: e.detail.value
    });
  },
  // 保存提交个人信息
  formSubmit(e){
    var that=this;
    var inputDetail = e.detail.value
   if(inputDetail.userName == '' || inputDetail.useTele == '' || inputDetail.userJob=='' || inputDetail.userCompons == '' || inputDetail.userIntroduce==''){
     wx.showModal({
       title: '温馨提示',
       content: "请把个人信息补充完整",
       showCancel: false,
       success: function (res) {
         console.log(res)
         if (res.confirm) {
            console.log("qudedsdx")

         }
       }
     })
   }else{
     var nameReg = /[`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘’，。、]/im;//特殊字符
     var teleReg = /^((1[3,5,8][0-9])|(14[5,7])|(17[0,6,7,8])|(19[7]))\d{8}$/;
    //  姓名验证
     if ((nameReg.test(inputDetail.userName))){
       wx.showModal({
         title: '温馨提示',
         content: "姓名不能包含特殊字符",
         showCancel: false,
         success: function (res) {
           console.log(res)
         }
       })
     } else if (inputDetail.useTele.length != 11 || (!teleReg.test(inputDetail.useTele))){
       wx.showModal({
         title: '温馨提示',
         content: "请输入正确的手机号",
         showCancel: false,
         success: function (res) {
           console.log(res)
         }
       })
     } else if ((nameReg.test(inputDetail.userJob))){
       wx.showModal({
         title: '温馨提示',
         content: "职位不能包含特殊字符",
         showCancel: false,
         success: function (res) {
           console.log(res)
         }
       })
     } else if ((nameReg.test(inputDetail.userCompons))) {
       wx.showModal({
         title: '温馨提示',
         content: "公司不能包含特殊字符",
         showCancel: false,
         success: function (res) {
           console.log(res)
         }
       })
     } else if ((nameReg.test(inputDetail.userIntroduce))) {
       wx.showModal({
         title: '温馨提示',
         content: "个人简介不能包含特殊字符",
         showCancel: false,
         success: function (res) {
           console.log(res)
         }
       })
     }else{
       console.log(that.data.selectAllStatus)
       //  验证通过，填写信息，并且需要投简历
       if (that.data.selectAllStatus==true){
        //  that.setData({
        //    showModalStatus:true
        //  })
         that.userInfoAndTou(inputDetail)

     
       } else { //  验证通过，只是填写信息，不投简历
         console.log('只是填写信息，不投简历')
         that.postUserData(inputDetail)
       }
     }
   }
  
  },
  // 获取简历信息
  getJianLiInfo(){
    var that=this;
    wx.request({
      url: serveUrl + "get_cv_data_by_openid",
      data: {
        openid: app.globalData.opeid
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success: function (res) {
        console.log("oooooo")
        console.log(res.data.data[0])
        that.setData({
          userInfos: res.data.data[0]
        })
        var arrYear = that.data.yearNameArr
        for (var i = 0; i < arrYear.length;i++){
          if (arrYear[i] == res.data.data[0].job_age){
            console.log(arrYear[i])
            that.setData({
              yearIndex: i
            });
          }
        }
        
      }
    })
  },
  // 取消遮罩
  powerDrawer: function () {
    var that = this;
    that.setData(
      {
        showModalStatus: false
      })

  },
  // 保存信息并且投递
  userInfoAndTou(inputDetail){
    var that=this;
    console.log(inputDetail);
    wx.request({
      url: serveUrl + "insert_cv_data",
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      data: {
        name: inputDetail.userName,
        tel: inputDetail.useTele,
        job_position: inputDetail.userJob,
        company: inputDetail.userCompons,
        job_age: inputDetail.userWorkYear,
        introduction: inputDetail.userIntroduce,
        openid: app.globalData.opeid,
        recruitid: that.data.recruitId
      },
      success: function (res) {
        console.log("我是用来保存用户信息的")
        console.log(res);
        wx.navigateBack();
         wx.showToast({
          title: '请稍后',
          icon: 'success',
          duration: 5000
        });
        wx.navigateBack()

      }
    })
  },
  // 只保存信息
  postUserData(inputDetail){
    console.log("lll")
    console.log(inputDetail);
    wx.request({
      url: serveUrl + "get_recruit_data",
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      data: {
        name: inputDetail.userName,
        tel: inputDetail.useTele,
        job_position: inputDetail.userJob,
        company: inputDetail.userCompons,
        job_age: inputDetail.userWorkYear,
        introduction: inputDetail.userIntroduce,
        openid: app.globalData.opeid
      },
      success: function (res) {
        console.log("我是用来保存用户信息的")
        console.log(res);
        wx.showToast({
          title: '请稍后',
          icon: 'success',
          duration: 5000
        });
        // wx.navigateBack()
        
        // that.setData({
        //   listData: res.data.data
        // });
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
 
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function (e) {
    console.log("wqdfuhhdddddddddddddddd");
    console.log(e)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})